package com.glela.order.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.glela.order.dao.OrderTokenDao;
import com.glela.order.model.OrderToken;
import com.glela.order.service.OrderTokenService;

@Service
public class OrderTokenServiceImpl implements OrderTokenService {
	@Autowired
	private OrderTokenDao orderTokenDao;

	@Override
	public boolean isTokenValid(String token) {
		if (StringUtils.isEmpty(token)) {
			return false;
		}
		OrderToken selectById = orderTokenDao.selectById(token);
		return selectById != null && selectById.getStatus() == 1;
	}

	@Override
	public boolean removeToken(String token) {
		if (StringUtils.isEmpty(token)) {
			return false;
		}
		return orderTokenDao.delete(token) == 1;
		//		return orderTokenDao.updateTokeByStatus(token, 2, 1) == 1;
	}

	@Override
	public boolean addToken(String token, long userId) {
		if (StringUtils.isEmpty(token)) {
			return false;
		}
		OrderToken addOrderToken = new OrderToken();
		addOrderToken.setOrderKey(token);
		addOrderToken.setUserId(userId);
		addOrderToken.setStatus(1);
		return orderTokenDao.saveToken(addOrderToken) == 1;
	}

}
